﻿@using CoralNode.Core.Entity.Domains.Node
@using CoralNode.Core.Entity.Domains.Prototype
@using CoralNode.MvcClient.Helper
@model Coralcode.Base.Entity.Domains.PagedDomainList<NodeDomain>
@{
    Layout = null;
    PrototypeDomain prototype = ViewBag.Prototype;
}
<div class="pj_overlay" id="pj_overlay">
    <div class="olay_h">选择@(prototype.Title)</div>
    <div class="olay_b clearfix  olay_t">
        <div class="cnt h_48  pb_1 clearfix adduser_box">
            <div class="funsearch  pt_0-5 pb_0-5 extra_normal" data-js="filemanage" data-delhref="#"
                 data-savehref="#">
                <div class="f_r">
                    <form class="j_form" method="GET" action="/m/node/singlerelation">
                        <label class="search f_l mr_1 clearfix">
                            <input type="hidden" name="prototypeId" value="@ViewBag.Query.PrototypeId"/>
                            <input type="hidden" name="categoryId" value="@ViewBag.Query.CategoryId"/>
                            <input class="text w_18" name="Title" value=""><input type="submit" value="" class="submit">
                        </label>
                    </form>
                </div>
            </div>
            <div>
                <div class="olay_cnt clearfix js_user_select">
                    <div class="left clearfix">
                        <div class=" p_0-5 olay_b-1 clearfix">
                            <div class="select_owner w_19" data-meta="{value:'@prototype.Id',name:'PrototypeId',fun:'selectPrototype',btn:'<span class=\'ico ico_arraw_down_blank\'></span>',clickbtn:'<span class=\'ico ico_arraw_up_blank\'></span>'}">
                                <ul>
                                    @foreach (var item in ViewBag.PrototypeList)
                                    {
                                        <li data-value="@item.Id">@item.Title</li>
                                    }
                                </ul>
                            </div>
                        </div>
                        <ul class="list_word p_0-5 pb_0 f_l w_19">
                            <li class="current"><a class="js-letter" href="/m/node/singlerelation?prototypeId=@prototype.Id">全部</a></li>
                            @foreach (var item in prototype.CategoryList)
                            {
                                <li><a class="js-letter" href="/m/node/singlerelation?categoryId=@item.Id">@item.Title</a></li>
                            }
                        </ul>
                    </div>
                    <div class="right">
                        <div class="membersort f_no">
                            <div class="sortbox clearfix js-ajax-content">
                                @if (Model.TotalItems == 0)
                                {
                                    <div class="nodata">
                                        <div class="nodata_error">
                                            <i class="ico_d_error"></i>
                                            @{
                                                var category = prototype.CategoryList.FirstOrDefault();
                                                var categoryId = ViewBag.Query.CategoryId > 0 ? ViewBag.Query.CategoryId : category.Id;
                                            }
                                            <p class="mt_3">没有找到相关内容，点击<a target="_blank" id="j-c-node" href="/m/node/createNode?categoryId=@categoryId" class="btn btn_green_normal w_8 f_no">创建@(prototype.Title)</a>去创建</p>
                                        </div>
                                        <div style="display: none">
                                            <i class="ico_d_good"></i>
                                            <p class="mt_3">若创建完成，点击<a href="/m/node/singlerelation?categoryId=@categoryId" class="j-refresh btn btn_green_normal w_8 f_no">刷新</a>重新搜索</p>
                                        </div>
                                    </div>
                                }
                                @foreach (NodeDomain item in Model.Items) {

                                    <div class="memberbox j_checkbox" itemid="@item.Id">
                                        <div class="box">
                                            <label class="checkbox_own">
                                                <input type="checkbox" value="@item.Id">
                                            </label>
                                            <div class="t">
                                                @UtilHelper.ToShortTitle(item.Title, 60)
                                            </div>
                                            <div class="cnt">
                                                <span class="mr_2">@item.Id</span><span>分类：@item.Category.Title</span>
                                                <div class="r time">
                                                    @item.CreateTime.ToString("MM-dd HH:mm")
                                                    <a class="ml_1 ico ico_eye_@item.Status btn" style="position: relative;top:-1px"></a>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                }
                                @Html.IncludePage("CoralAdmin/Shared","Pager2",new{list=Model})
                            </div>

                        </div>
                    </div>
                </div>
                </div>
            <div class="usernum">
                最多可选<span class="btn btn_green_small_style3  f_no ml_0-5 mr_0-5 j_maxNumber"></span>个,
                已选择<span class="btn btn_green_small_style3  f_no ml_0-5 mr_0-5 js_manage_selectnum">0</span>个
                <input type="hidden" name="selectnum" value="" />
            </div>
        </div>
        <div class="olay_btn clearfix">
            <div class="r">
                <a class="btn btn_green_normal w_6 js-member-save" href="">保存</a>
                <a class="btn btn_gray_normal w_6 ml_1 js-close" href="javascript:void(0);">取消</a>
            </div>
        </div>
    </div>
</div>
    <input type="hidden" id="js_number" />
<script>
    
    var $outer = $("#pj_overlay").parents(".d-outer");
    var params = $outer.data("params");
    var nodeIds = params.current.split(',');
    function selectPrototype(obj) {
        var prototypeId = obj.find('input[name="PrototypeId"]').val();
        $('.js_user_select').parent().load("/m/node/singlerelation?PrototypeId=" + prototypeId + " .js_user_select", function () {
            $('.select_owner').dropdown_owner();
            $(".js_user_select").each(function () {
                var $this = $(this);
                $('.membersort .memberbox').each(function (i, n) {
                    if (nodeIds.hasobj($(n).attr("itemId"))) {
                        $(n).addClass("memberboxcurrent");
                        $(n).find('.checkbox_own').addClass('selected');
                    }
                });
                changeManageEvent($this, 1);
            });
        });
    }

    $('.j_checkbox').live("click", function () {
        var $this = $(this);
        var isCheck =$this.hasClass('memberboxcurrent');
        if (isCheck) {
            nodeIds.push($this.attr("itemId"));
        } else {
            nodeIds.removeobj($this.attr("itemId"));
        }
    });
    $('.j_form').submit(function (e) {
        e.preventDefault();
        var element = $(this).attr("action") + " .js-ajax-content";
        $('.js-ajax-content').parent().load(element, $(this).serialize(), function () {
            $(".js_user_select").each(function () {
                var $this = $(this);
                changeManageEvent($this, 1);
            });
        });
    });
    $('.js-member-save').click(function (e) {
        e.preventDefault();
        var userIds = nodeIds;
        var titles = new Array();
        if (params != null && params.okfun != null && typeof (params.okfun) === 'function') {
            eval("params.okfun(userIds,titles)");
        }
    });
    $('.js-close').click(function () {
        $('a.d-close').trigger("click");
    });
    $(".select_owner").dropdown_owner();
    //自定义checkbox
    $(".js_user_select").each(function () {
        var $this = $(this);
        changeManageEvent($this, 1);

    });
    $('.js-letter').live("click",function (e) {
        e.preventDefault();
        $(this).parent().addClass('current').siblings().removeClass('current');
        var element = $(this).attr("href") + " .js-ajax-content";
        $('.js-ajax-content').parent().load(element, function () {
            $(".js_user_select").each(function () {
                var $this = $(this);
                var currentText = 0;
                $('.membersort .memberbox').each(function (i, n) {
                    if (nodeIds.hasobj($(n).attr("itemId"))) {
                        $(n).addClass("memberboxcurrent");
                        $(n).find('.checkbox_own').addClass('selected');
                        currentText++;
                    }
                });
                $('.js_manage_selectnum').text(currentText);
                changeManageEvent($this, 1);
            });
        });
    });
    $('.j-refresh').click(function (e) {
        e.preventDefault();
        var element = $(this).attr("href") + " .js-ajax-content";
        $('.js-ajax-content').parent().load(element, function () {
            $(".js_user_select").each(function () {
                var $this = $(this);
                changeManageEvent($this, 1);
            });
            $('.js_manage_selectnum').text(0);
        });
    });
    $('.pager a').live("click", function(e) {
        e.preventDefault();
        var element = $(this).attr("href") + " .js-ajax-content";
        $('.js-ajax-content').parent().load(element,function() {
            $(".js_user_select").each(function () {
                var $this = $(this);
                var currentText = 0;
                $('.membersort .memberbox').each(function (i, n) {
                    if (nodeIds.hasobj($(n).attr("itemId"))) {
                        $(n).addClass("memberboxcurrent");
                        $(n).find('.checkbox_own').addClass('selected');
                        currentText++;
                    }
                });
                $('.js_manage_selectnum').text(currentText);
                changeManageEvent($this, 1);
            });
        });

    });
    function changeManageEvent($currentbox, type) {
        type = type || 1;
        var $items = $currentbox.find(".memberbox:not('.adduser')");
        if (type == 1) {
            $currentbox.next().show();
            var $num = $(".js_manage_selectnum");
            $items.find(".checkbox_own").show().click(function () {
                $(this).trigger("click.own");
            });
            $items.css("cursor", "pointer").bind("click.change", function (e) {
                e.preventDefault();
                var $this = $(this);
                var nodelength = $('#js_number').val();
                var number = nodeIds.length;
                if (nodelength == "无限制") {

                } else if (nodelength == "1") {
                    $this.siblings(".memberboxcurrent").trigger("click");
                } else {
                    if ($this.hasClass("memberboxcurrent") || $('#js_number').val() > number) {

                    } else {
                        alert("超出限制");
                        return;

                    }
                }
                $this.find(".checkbox_own").trigger("click.own");
                $this.hasClass("memberboxcurrent") ? $this.removeClass("memberboxcurrent") : $this.addClass("memberboxcurrent");
                number = nodeIds.length;
                $num.text(number);

            });
           
            $currentbox.find(".adduser").hide();
        }
        else {

            $items.filter(".memberboxcurrent").trigger("click");
            $items.find(".checkbox_own").hide();
            $items.unbind("click.change");
            $currentbox.find(".adduser").show();
            $currentbox.next().hide();
            $currentbox.next().find(".js_manage_selectnum").text("0")
        }
    }
</script>
